package com.mall.seckil.interceptor;

import cn.hutool.core.text.AntPathMatcher;
import com.mall.common.constant.AuthorConstant;
import com.mall.common.vo.UserInfoVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: 谢君臣
 * @Date: 2022/8/8-14:26
 * @version: 1.0
 * @function:
 */
@Component
public class SeckillIndexInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserInfoVo> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        UserInfoVo userInfoVo = (UserInfoVo) session.getAttribute(AuthorConstant.LOGIN_USER);
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match = pathMatcher.match("/seckill/kill/sku", request.getRequestURI());
        if (userInfoVo == null && match){
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://author.mall.com");
            return false;
        }
        threadLocal.set(userInfoVo);
        return true;
    }
}
